package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.SaleHouseStepPagerAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.BuyHouseStepBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.AuditProgressView;
import com.weixin.paleituo.view.ForbidScrollViewpager;
import com.weixin.paleituo.view.LoadingPager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 购房进度
 */
public class BuyHouseStepActivity extends BaseActivity {

    @BindView(R.id.ll_audit_content)
    LinearLayout mLlAuditContent;
    @BindView(R.id.viewPager_activity_sale_house_step)
    ForbidScrollViewpager mViewPagerActivitySaleHouseStep;
    private SaleHouseStepPagerAdapter mSaleHouseStepPagerAdapter;
    private AuditProgressView mPreHouseView;
    private AuditProgressView mSignedHouseView;
    private AuditProgressView mAccepetHouseView;
    private AuditProgressView mIntakeHouseView;
    private LoadingPager mLoadingPager;
    private String mId="";
    private String mPhone;

    @Override
    protected void initView() {

        mLoadingPager = new LoadingPager(this) {
            @Override
            protected void close() {
                finish();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(BuyHouseStepActivity.this, R.layout.activity_sale_house_step);
            }
            @Override
            protected void reLoading() {
                retry();

            }
        };
      //  setContentView(R.layout.activity_sale_house_step);
       setContentView(mLoadingPager);

    }

    /**
     * 重试
     */
    private void retry() {

        Observable<StatusCode<BuyHouseStepBean>> buyHouseDetailsCodeObservable = ApiUtils.getApi().getBuyHouseDetails(mId,mPhone,BaseApplication.sUserInfoBean.getUserId(),BaseApplication.sUserInfoBean.getToken());

        HttpUtil.getInstance().toSubscribe(buyHouseDetailsCodeObservable, new ProgressSubscriber<BuyHouseStepBean>(this) {

            @Override
            protected void _onNext(StatusCode<BuyHouseStepBean> t) {
                BuyHouseStepBean buyHouseStepBean = t.getData();
                if (buyHouseStepBean!=null){
                    LogUtils.i(TAG,":::::"+buyHouseStepBean.toString());
                    mLoadingPager.showPagerView(Contans.STATE_SUCCEED);
                    setData(buyHouseStepBean);

                }else {
                    mLoadingPager.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                mLoadingPager.showPagerView(Contans.STATE_ERROR);

            }
        },"",lifecycleSubject,false,true);

    }

    /**
     * 设置数据
     * @param buyHouseStepBean
     */
    private void setData(BuyHouseStepBean buyHouseStepBean) {
        /**预定*/
        boolean isReserve = buyHouseStepBean.getIsReserve().equals("1")?true:false;
        /**签约*/
        boolean isSign = buyHouseStepBean.getIsSign().equals("1")?true:false;
        /**收房*/
        boolean isPurchase = buyHouseStepBean.getIstake().equals("1")?true:false;
        /**是否入住*/
        boolean isCheck = buyHouseStepBean.getIscheck().equals("1")?true:false;

        mPreHouseView = createView(3, isReserve, isSign, true, false, "预定", R.mipmap.tjdj);
        mLlAuditContent.addView(mPreHouseView);
        mSignedHouseView = createView(3, isSign, isPurchase, false, false, "签约", R.mipmap.qdht);
        mLlAuditContent.addView(mSignedHouseView);
        mAccepetHouseView = createView(3, isPurchase, isCheck, false, false, "收房", R.mipmap.jf);
        mLlAuditContent.addView(mAccepetHouseView);
        mIntakeHouseView = createView(3, isCheck, false, false, true, "入住", R.mipmap.rz);
        mLlAuditContent.addView(mIntakeHouseView);

        mSaleHouseStepPagerAdapter = new SaleHouseStepPagerAdapter(getSupportFragmentManager(),buyHouseStepBean);
        mViewPagerActivitySaleHouseStep.setAdapter(mSaleHouseStepPagerAdapter);
        mViewPagerActivitySaleHouseStep.setCurrentItem(0, false);


        mPreHouseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //预定

                if (mPreHouseView.isCurrentComplete()) {

                    mViewPagerActivitySaleHouseStep.setCurrentItem(0, false);
                }
            }
        });
        mSignedHouseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSignedHouseView.isCurrentComplete()) {

                    //签约
                    mViewPagerActivitySaleHouseStep.setCurrentItem(1, false);
                }
            }
        });
        mAccepetHouseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAccepetHouseView.isCurrentComplete()) {
                    //收房
                    mViewPagerActivitySaleHouseStep.setCurrentItem(2, false);
                }
            }
        });
        mIntakeHouseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIntakeHouseView.isCurrentComplete()) {
                    //入住
                    mViewPagerActivitySaleHouseStep.setCurrentItem(3, false);
                }
            }
        });


    }


    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mId = intent.getStringExtra(Contans.INTENT_DATA);
        mPhone = intent.getStringExtra(Contans.INTENT_EXTRA);

        retry();
    }
    @Override
    protected void initEvent() {
        super.initEvent();
    }

    public static void startActivity(Context context,String id,String phone) {
        Intent intent = new Intent(context, BuyHouseStepActivity.class);
        intent.putExtra(Contans.INTENT_DATA,id);
        intent.putExtra(Contans.INTENT_EXTRA,phone);
        context.startActivity(intent);

    }
    
    /**
     * 创建步骤
     *
     * @param stepCount         一页显示个数
     * @param isCurrentComplete 当前步骤是否完成
     * @param isNextComplete    下一个步骤是否完成
     * @param isFirstStep       是否为第一步
     * @param isLastStep        是否为最后一步
     * @param text              文字描述
     * @return
     */
    public AuditProgressView createView(int stepCount, boolean isCurrentComplete, boolean isNextComplete,
                                        boolean isFirstStep, boolean isLastStep, String text, int showIconId) {
        AuditProgressView view = new AuditProgressView(this);
        view.setStepCount(stepCount);
        view.setIsCurrentComplete(isCurrentComplete);
        view.setIsNextComplete(isNextComplete);
        view.setIsFirstStep(isFirstStep);
        view.setIsLastStep(isLastStep);
        view.setStepIcon(showIconId);
        view.setText(text);
        return view;
    }

    @OnClick(R.id.iv_back_activity_sale_house_step)
    public void onMIvBackActivitySaleHouseStepClicked() {
        if (BaseApplication.isLoginSuccess){
            BuyHouseListActivity.startActivity(this);
            finish();


        }else {
            LoginActivity.startActivity(this,true);
        }



    }

}
